import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;

public class HelloServer {
	private static final int	PORT	= 2019;
	
	public static void main(String args[]) {
		test4();
	}
	
	public static void test4() {
		// Create and install a security manager
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}
		
		try {
			// Create SSL-based registry
			RMIClientSocketFactory csf = new SslRMIClientSocketFactory();
			RMIServerSocketFactory ssf = new SslRMIServerSocketFactory();
			Registry registry = LocateRegistry.createRegistry(PORT, csf, ssf);
			
			// Registry registry = LocateRegistry.createRegistry(PORT);
			
			HelloImpl obj = new HelloImpl(PORT, csf, ssf);
			
			// Bind this object instance to the name "HelloServer"
			//registry.bind("HelloServer", obj);
			registry.bind("rmi://192.168.1.202:"+PORT+"/HelloServer", obj);
			// Naming.rebind("//localhost:"+PORT+"/HelloServer",obj);
			
			System.out.println("HelloServer bound in registry");
		} catch (Exception e) {
			System.out.println("HelloImpl err: " + e.getMessage());
			e.printStackTrace();
		}
	}
	
	public static void test3() {
		// Create and install a security manager
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}
		
		try {
			// Create SSL-based registry
			RMIClientSocketFactory csf = new RMISSLClientSocketFactory();
			RMIServerSocketFactory ssf = new RMISSLServerSocketFactory();
			Registry registry = LocateRegistry.createRegistry(PORT, csf, ssf);
			
			// Registry registry = LocateRegistry.createRegistry(PORT);
			
			HelloImpl obj = new HelloImpl(PORT, csf, ssf);
			
			// Bind this object instance to the name "HelloServer"
			registry.bind("HelloServer", obj);
			// Naming.rebind("//localhost:"+PORT+"/HelloServer",obj);
			
			System.out.println("HelloServer bound in registry");
		} catch (Exception e) {
			System.out.println("HelloImpl err: " + e.getMessage());
			e.printStackTrace();
		}
	}
	
	public static void test2() {
		// Create and install a security manager
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}
		
		try {
			// Create SSL-based registry
			// Registry registry = LocateRegistry.createRegistry(PORT, new RMISSLClientSocketFactory(), new
			// RMISSLServerSocketFactory());
			Registry registry = LocateRegistry.createRegistry(PORT);
			
			HelloImpl obj = new HelloImpl();
			
			// Bind this object instance to the name "HelloServer"
			registry.bind("HelloServer", obj);
			// Naming.rebind("//localhost:"+PORT+"/HelloServer",obj);
			
			System.out.println("HelloServer bound in registry");
		} catch (Exception e) {
			System.out.println("HelloImpl err: " + e.getMessage());
			e.printStackTrace();
		}
	}
	
	/*
	 * no security manager
	 */
	public static void test1() {
		try {
			Registry registry = LocateRegistry.createRegistry(PORT);
			
			HelloImpl obj = new HelloImpl();
			
			// Bind this object instance to the name "HelloServer"
			registry.bind("HelloServer", obj);
			// Naming.rebind("//localhost:"+PORT+"/HelloServer",obj);
			
			System.out.println("HelloServer bound in registry");
		} catch (Exception e) {
			System.out.println("HelloImpl err: " + e.getMessage());
			e.printStackTrace();
		}
	}
}
